import { WidgetBaseSession } from './base'
import {fromAny, toAny, Int32Value, StringValue} from "../../proto";
import { Message } from 'protobufjs'
export class WidgetImageSession extends WidgetBaseSession {
    private image: HTMLImageElement
    constructor(render: HTMLElement) {
        super('Image', render)
    }
    createWidget(...argv): HTMLElement {
        this.image = document.createElement('img')
        this.image.src = fromAny(argv[0], StringValue)
        return this.image
    }
    onReceive(name: String, sessionId: number, futureId: number, ...argv) {
        switch (name) {
            case 'getSrc':
                return this.getSrc(sessionId, futureId)
            case 'setSrc':
                return this.setSrc(...argv)
        }
        super.onReceive(name, sessionId, futureId, ...argv);
    }
    private getSrc(sessionId: number, futureId: number) {
        const src = toAny(this.image.src, Int32Value)
        this.onGetSrc(sessionId, futureId, src)
    }
    private onGetSrc(sessionId: number, futureId: number, src: Message) {
        this.sendAction('onGetSrc', sessionId, futureId, src)
    }
    private setSrc(...argv) {
        this.image.src = fromAny(argv[0], Int32Value)
    }
}